#! /usr/bin/env python
# -*- coding: utf-8

##	  Copyright 2012, Geoffrey GROFF and Dimitri SEGARD
##	  This file is part of otaku-loader.
##
##    otaku-loader is free software: you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation, either version 3 of the License, or
##    (at your option) any later version.
##
##    otaku-loader is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with otaku-loader.  If not, see <http://www.gnu.org/licenses/>.

import tkinter as tk
import threading
import time


import globales

def thread_func():

    global liste_dl
    global thread_dls

    max = 20
    current = 0

    while not thread_dls.stop :
        if liste_dl and not globales.pause :
            episode = liste_dl.pop(0)
            print(episode)
            globales.dl.prepareAffichage()
            globales.dl.dlFile(globales.dl.getDdlLink(episode['id'],episode['id_anime']))
            globales.dl.defilList()
            current = 0
            while current < max:
                time.sleep(1)
                current += 1
        else :
            time.sleep(0.05)
        
thread_dls=threading.Thread(target=thread_func)
liste_dl = []

def run():

    global thread_dls

    if not thread_dls.is_alive(): 
        
        thread_dls = None
        thread_dls=threading.Thread(target=thread_func)
            
        thread_dls.stop=False
        thread_dls.start()


def stop():
    global thread_dls
    if thread_dls.is_alive():
	    thread_dls.stop=True
	    thread_dls.join()

def add_dl(lien):
    global liste_dl
    if lien not in liste_dl :
        liste_dl.append(lien)
        return True
        
    else :
        return False

